package com.yuxl.ai.spg.repository;

import com.yuxl.ai.spg.entity.AiSpgSchema;
import com.yuxl.common.core.annotation.PostgreSqlRepository;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@PostgreSqlRepository
public interface AiSpgSchemaRepository extends ReactiveCrudRepository<AiSpgSchema, Long> {

    @Query("""
            SELECT count(*) FROM ai_spg_schema
            WHERE 1=1
            AND (:#{#entity.name} IS NULL OR name LIKE CONCAT('%', :#{#entity.name}, '%'))
            """)
    Mono<Integer> count(AiSpgSchema entity);

    @Query("""
            SELECT * FROM ai_spg_schema
            WHERE 1=1
            AND (:#{#entity.name} IS NULL OR name LIKE CONCAT('%', :#{#entity.name}, '%'))
            ORDER BY create_time DESC
            LIMIT :limit OFFSET :offset
            """)
    Flux<AiSpgSchema> page(AiSpgSchema entity, long limit, long offset);

    @Query("""
            SELECT * FROM ai_spg_schema
            WHERE 1=1
            AND (:#{#entity.name} IS NULL OR name LIKE CONCAT('%', :#{#entity.name}, '%'))
            ORDER BY create_time DESC
            """)
    Flux<AiSpgSchema> list(AiSpgSchema entity);


    // 添加根据id 的 update语句
    @Query("""
            UPDATE ai_spg_schema
            SET name = :#{#entity.name},
                update_time = :#{#entity.updateTime}
            WHERE id = :#{#entity.id}
            """)
    Mono<Integer> updateById(AiSpgSchema entity);

}
